<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hide(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="hide(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="shanchu(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

    </div>
    <el-dialog :visible.sync="showDialog" title="弹层标题" @click="resetForm">
      <!-- 表单内容 -->
      <el-form ref="form" label-width="100px" :model="fromData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="fromData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="fromData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="fromData.description" />
        </el-form-item>
        <el-form-item label="权限启用" prop="enVisible">
          <el-switch
            v-model="fromData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="hSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, delPermission, updatePermission, getPermissionDetail } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [], // 表格数据
      showDialog: false, // 是否显示弹层
      isEdit: false, // 是否是编辑  fasle是添加，true是编辑
      fromData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    }
  },
  created() {
    this.loadPermissionList()// 打开加载页面
  },
  methods: {
    async loadPermissionList() { // 加载
      const res = await getPermissionList()
      console.log(res)
      this.list = tranListToTreeData(res.data)
    },
    async doAdd() { // 更新保存
      try {
        const res = await addPermission(this.fromData)
        console.log(res)
        this.loadPermissionList()
        this.showDialog = false
        this.$message.success('添加成功')
      } catch (err) {
        console.log(err)
        // this.$message.error('添加失败')
      }
    },
    hSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.doAdd()// 兜底验证然后更新保存
        }
        if (this.isEdit) {
          this.doEdit()// 发送编辑提交请求
        } else {
          this.doAdd()// 发送添加请求
        }
      })
    },
    resetForm() { // 清空表单检验
      this.fromData = {}
      this.$refs.form.resetFields()// 清空校验
    },
    async shanchu(id) { // 删除成功
      try {
        const res = await delPermission(id)
        console.log(res)
        this.$message.success('删除成功')
        this.loadPermissionList()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    async hEdit(id) { // 编辑
      const res = await getPermissionDetail(id)
      console.log(res)
      this.showDialog = true
      this.isEdit = true
      this.fromData = res.data// 将他进行数据回填
    },
    async doEdit() { // 编辑提交
      try {
        const re = await updatePermission(this.fromData)// 没有地方去拿id，所有去fromdata里去拿
        console.log(re)

        this.$message.success('编辑成功')
        this.loadPermissionList()
        this.showDialog = false
        this.fromData = {}// 无论最后结果成功与否都直接清空表单框，
      } catch (err) {
        console.log(err)
        this.$message.error('编辑失败')
        this.fromData = {}
      }
    },

    hide(type, id) { // 2,id 2区分按钮，id是子树形组件需要id,,,,,// 1.0 0是父，1是针对页面上的按钮，区分
      console.log(id, type)
      this.isEdit = false// false是添加
      this.showDialog = true
      this.fromData.type = type
      this.fromData.pid = id
    }

  }
}
</script>
